package com.example.webapp.controler;

import com.example.webapp.bean.QuestionBank;
import com.example.webapp.bean.User;
import com.example.webapp.service.QuestionService;
import com.example.webapp.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.Base64;
import java.util.Random;
import static java.lang.Math.abs;


@RestController
public class QuestionControler {
    @Autowired
    private QuestionService questionService;

    @RequestMapping(value = "/question") // 访问路径
    public String getQuestion() {
        Integer RandId=abs((new Random().nextInt())%3)+1;
        System.out.println(RandId);
        QuestionBank NewQuestion=questionService.getQuestionById(RandId);
        if(NewQuestion==null)
            return "success";

        Base64.Encoder encoder= Base64.getEncoder();
        String PicBase64 = encoder.encodeToString(NewQuestion.getQuestion_file());
        String PicId= NewQuestion.getQuestion_id().toString();

        return "{"+"\"qid\":"+"\""+PicId+"\""+
                ","+"\"qimage\":"+"\""+PicBase64+"\""+"}";
    }
}